package com.hs.carrental.controller;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.hs.carrental.common.Result;
import com.hs.carrental.dto.LoginDto;
import com.hs.carrental.dto.RegisterDto;
import com.hs.carrental.dto.customer.*;
import com.hs.carrental.service.ICustomerService;
import com.hs.carrental.util.Helper;
import com.hs.carrental.util.PasswordEncrypter;
import com.hs.carrental.vo.LoginVo;
import com.hs.carrental.vo.UpdatePasswordVo;
import com.hs.carrental.vo.customer.CustomerPageVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.io.UnsupportedEncodingException;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lyy
 * @since 2021-03-17
 */
@RestController
@RequestMapping("/carrental/customer")
public class CustomerController {

    @Autowired
    private ICustomerService customerService;

    /**
     * 注册
     * @param registerDto
     * @return
     */
    @PostMapping("/register")
    public Result<Object> register(@RequestBody RegisterDto registerDto) throws UnsupportedEncodingException {
        //校验用户名是否重复
        if (customerService.checkUserName(registerDto.getCustomerAccount(),1)){
            return Result.failed("用户名已存在");
        }
        //注册
        boolean register = customerService.register(registerDto);
        return register?Result.ok():Result.failed("注册失败");
    }

    /**
     * 登录
     * @param loginDto
     * @return
     */
    @PostMapping("/login")
    public Result<JSONObject> login(@RequestBody LoginDto loginDto) throws Exception {
        if (!customerService.checkUserName(loginDto.getUsername(),loginDto.getIdentity())){
            return Result.failed("用户名不存在");
        }
        JSONObject loginVo = customerService.login(loginDto);
        if (Helper.isEmpty(loginVo)){
            return Result.failed("用户名或密码错误");
        }
        return Result.ok(loginVo);
    }

    /**
     * 新增 编辑
     * @param customerSaveDto
     * @return
     * @throws UnsupportedEncodingException
     */
    @PostMapping("/save")
    public Result save(@RequestBody CustomerSaveDto customerSaveDto) throws UnsupportedEncodingException {
        if (customerService.save(customerSaveDto)){
            return Result.ok("操作成功");
        }
        return Result.ok("操作失败");
    }

    /**
     * 删除
     * @param customerDelDto
     * @return
     */
    @PostMapping("/delete")
    public Result delete(@RequestBody CustomerDelDto customerDelDto){
        if (Helper.isEmpty(customerDelDto)){
            return Result.failed("id为空");
        }
        if (customerService.removeById(customerDelDto.getCustomerId())){
            return Result.ok("删除成功");
        }
        return Result.failed("删除失败");
    }

    /**
     * 分页
     * @param customerPageDto
     * @return
     */
    @PostMapping("/getPage")
    public Result<IPage<CustomerPageVo>> getPage(@RequestBody CustomerPageDto customerPageDto){
        IPage<CustomerPageVo> page = customerService.getPage(customerPageDto);
        return Result.ok(page);
    }

    /**
     * 修改密码
     * @param updatePasswordDto
     * @return
     */
    @PostMapping("/updatePassword")
    public Result updatePassword(@RequestBody UpdatePasswordDto updatePasswordDto) throws Exception {
        UpdatePasswordVo updatePasswordVo = customerService.updatePassword(updatePasswordDto);
        if (updatePasswordVo.getCode().equals(200)){
            return Result.ok(updatePasswordVo.getMsg());
        }
        return Result.failed(updatePasswordVo.getMsg());
    }

    /**
     * 获取个人信息
     * @param getUserInfoDto
     * @return
     */
    @PostMapping("/getUserInfo")
    public Result<JSONObject> getUserInfo(@RequestBody GetUserInfoDto getUserInfoDto){
        return Result.ok(customerService.getUserInfo(getUserInfoDto));
    }

    /**
     * 修改个人信息
     * @param jsonObject
     * @return
     */
    @PostMapping("/updateUserInfo")
    public Result updateUserInfo(@RequestBody JSONObject jsonObject){
        if (Helper.isEmpty(jsonObject.get("identity"))){
            return Result.failed("个人标识为空");
        }
        if (customerService.updateUserInfo(jsonObject)){
            return Result.ok("修改成功");
        }
        return Result.failed("修改失败");
    }
}
